

load(file=paste(rawDatDir2,'obstetrics_all.RData', sep = ''))
hospitalCrosswalk = datCut[year==2012, mean(year), by = c("hospital", "hosp_id")]
setnames(hospitalCrosswalk, "hosp_id","hosp_id_alt")
hospitalCrosswalk$V1 = NULL
rm(datCut)

## Developed in fl_compute_deltas.R
load(paste0(datDir,"deltaDat_minPpl_all_5.RData"))

data = grpShrDatXi[is.finite(xiHat),][.id == "main",]
setnames(data,"nGrp","nZip")

zipPSA = c(33024,33068,33309,33311,33312,33313,33314,33317,33319,33321,33322,33323,33324,33325,33328,33330,33351)
ZIP_CLOSER = c(33325,33330,33328,33314,33024)
ZIP_CONSTANT = c(33323,33324,33317,33312)
ZIP_FARTHER = c(33321,33068,33351,33319,33309,33322,33313,33311)

POP_ADJ_CLOSER = (1990/1972)
POP_ADJ_CONSTANT = (1952/1971)
POP_ADJ_FARTHER = (6167/6293)

dataPSA <- data[pat_zip %in% zipPSA]
dataPSA$hosp_id_alt = as.character(dataPSA$hosp_id_alt)
dataPSA = merge(dataPSA, hospitalCrosswalk, by = c("hosp_id_alt"))


hospitalsInMarket = c("PLANTATION GENERAL HOSPITAL","BOCA RATON REGIONAL HOSPITAL", "BROWARD HEALTH MEDICAL CENTER",
                      "BROWARD HEALTH CORAL SPRINGS", "HOLY CROSS HOSPITAL INC", "MEMORIAL HOSPITAL MIRAMAR",  "MEMORIAL HOSPITAL WEST",
                      "MEMORIAL REGIONAL HOSPITAL","NORTHWEST MEDICAL CENTER","WEST BOCA MEDICAL CENTER" )
dataPSA[, includeHospital := (hospital %in% hospitalsInMarket)]
dataPSA[, hospShareOut2 := sum((includeHospital == FALSE) * hospShare), by = c("pat_zip")]
dataPSA[, hospShareOut := hospShareOut + hospShareOut2]
dataPSA$hospShareOut2 = NULL

dataPSAMkt = copy(dataPSA[includeHospital == TRUE,])
dataPSAMkt$includeHospital = NULL
dataPSAMkt$.id = NULL
dataPSAMkt$year = NULL
dataPSAMkt$hospShareNew = 0

## Adjust Delta for new outside option
dataPSAMkt[,deltaHat := log(hospShare) - log(hospShareOut)]
## New nZip to account for projections of market growth
dataPSAMkt[pat_zip %in% ZIP_CLOSER,nZipNew := nZip * POP_ADJ_CLOSER]
dataPSAMkt[pat_zip %in% ZIP_CONSTANT,nZipNew := nZip * POP_ADJ_CONSTANT]
dataPSAMkt[pat_zip %in% ZIP_FARTHER,nZipNew := nZip * POP_ADJ_FARTHER]

